package com.y17376065_yzh_FinalTest.rdtt.ui.activity;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.y17376065_yzh_FinalTest.rdtt.R;

public class RegisActivity extends Activity {

    //控件申明
    Button btn_login;
    EditText et_password, et_username;
    //用户名
    String username;
    //密码
    String password;

    private DB dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        //初始化数据库
        dbHelper = new DB(this, "1376065.db", null, 2);
        //初始化控件
        et_password = findViewById(R.id.et_password);
        et_username = findViewById(R.id.et_username);
        btn_login = findViewById(R.id.btn_login);

        //注册按钮点击事件
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = et_username.getText().toString();
                password = et_password.getText().toString();
                if (username.isEmpty()) {
                    Toast.makeText(RegisActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password.isEmpty()) {
                    Toast.makeText(RegisActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (CheckIsDataAlreadyInDBorNot(username)) {
                    Toast.makeText(RegisActivity.this, "该用户名已被注册，注册失败", Toast.LENGTH_SHORT).show();
                } else {
                    if (register(username, password)) {
                        Toast.makeText(RegisActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }
            }
        });
    }
    //新线程访问网络
                    new Thread(new Runnable(){
        @Override
        public void run() {
            AccessToServer login = new AccessToServer("http://172.20.10.13:8080/AndroidServers/RegisterServlet");
            //LoginToServer login = new LoginToServer();
            String result = login.doPost(new String[]{"edit_account","edit_password"},new String[]{name.getText().toString(),psd.getText().toString()});
            Message msg = new Message();
            msg.obj = result;
            myHandler.sendMessage(msg);
        }
    }).start();
}
            }
                    });

                    }
    //向数据库插入数据
    public boolean register(String username, String password) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        /*String sql = "insert into userData(name,password) value(?,?)";
        Object obj[]={username,password};
        db.execSQL(sql,obj);*/
        ContentValues values = new ContentValues();
        values.put("name", username);
        values.put("password", password);
        db.insert("username", null, values);
        db.close();
        return true;
    }

    //检验用户名是否已存在
    public boolean CheckIsDataAlreadyInDBorNot(String value) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String Query = "Select * from username where name =?";
        Cursor cursor = db.rawQuery(Query, new String[]{value});
        if (cursor.getCount() > 0) {
            cursor.close();
            return true;
        }
        cursor.close();
        return false;
    }
}

